package dict

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"
	"github.com/zeromicro/go-zero/core/logx"
)

type SimListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// SimList
func NewSimListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SimListLogic {
	return &SimListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SimListLogic) SimList() (resp []*types.SysSimDict, err error) {
	dictModel := model.SysDictModel{}
	items := make([]*model.SysDictModel, 0)
	l.svcCtx.GormClient.GormDb.Model(dictModel).
		Preload("DataList").
		Order("id asc").Find(&items)
	list := make([]*types.SysSimDict, 0)
	for _, item := range items {
		DataSimItems := make([]*types.SysSimDictData, 0)
		for _, data := range item.DataList {
			DataSimItems = append(DataSimItems, &types.SysSimDictData{
				Value:   data.Value,
				Label:   data.Label,
				TagType: data.Tag,
			})
		}
		list = append(list, &types.SysSimDict{
			Name:         item.Name,
			DictCode:     item.Code,
			DictDataList: DataSimItems,
		})
	}
	return list, nil
}
